#pragma once
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include "log.hpp"

class Util
{
public:
    //设置fd为非阻塞
    static bool NonBlocking(int fd)
    {
        int f = fcntl(fd, F_GETFL);
        if(f < 0)
        {
            log_message(Warning, "fcntl is error");
            return false;
        }

        fcntl(fd, fd | O_NONBLOCK);
    }

    static bool StringSplit(const string &str, const string &sep, vector<std::string> *result)
    {
        size_t start = 0;
        // + 20
        // "abcd efg" -> for(int i = 0; i < 10; i++) !=  for(int i = 0; i <= 9; i++)
        while (start < str.size())
        {
            auto pos = str.find(sep, start);
            if (pos == string::npos) break;
            result->push_back(str.substr(start, pos-start));
            // 位置的重新reload
            start = pos + sep.size();
        }
        if(start < str.size())  result->push_back(str.substr(start));
        return true;
    }
};